/**
 *
 */
package jMiiEditor.gui.preferences;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Properties;
import java.util.StringTokenizer;
import utils.BinStuffs;
import cx.ath.choisnet.lang.introspection.method.IVIgnore;

/**
 * @author CC
 *
 */
public class Preferences
    implements Serializable
{
    private static final long serialVersionUID = 1L;
    private final static String SYSTEM_ID = "wii.system.id";
    private final static String DEFAULT_CREATOR = "mii.default.creator";
    private final static File   FILE  = new File( new File( System.getProperty("user.home") ), "jMiiEditorPreferences.properties" );
    /** @serial */
    private final Properties prop;

    public Preferences()
    {
        prop = new Properties();
    }

    public void load() throws IOException
    {
        try {
            InputStream is = new FileInputStream( FILE );
            prop.load( is );
            is.close();
            }
        catch( FileNotFoundException noPrefs ) {
            // File not exist
        }
    }

    public void save() throws IOException
    {
        OutputStream is = new FileOutputStream( FILE );
        prop.store( is, "jMiiEditor default values" );
    }

    @IVIgnore //public int getSystemId()
    public byte[] getSystemId()
    {
        String hex = prop.getProperty( SYSTEM_ID );

        if( hex != null ) {
            byte[] nVal = BinStuffs.hexStringToByteArray( hex );

            if( nVal.length == 4 ) {
                return nVal;
                }
//            return Integer.parseInt( hex, 16 );
            }

        byte[] defaultValue = { 0,0,0,0 };
        return defaultValue;
    }

    @IVIgnore //@IVInt() //public void setSystemId(int systemId)
    public void setSystemId(byte[] systemId)
    {
        String hex = BinStuffs.toHexString(systemId);
        //String hex = String.format( "%08X", systemId );
        prop.setProperty( SYSTEM_ID, hex );
    }

    public void setFMTSystemId(String systemId)
    {
        byte[]          bytes = new byte[ 4 ];
        StringTokenizer st    = new StringTokenizer(systemId,"-");
        int             i     = 0;

        while( st.hasMoreTokens() ) {
            bytes[ i++ ] = (byte)Integer.parseInt( st.nextToken(), 16 );
        }

        //String hex  = systemId.replaceAll( "_", "" );
        //int intValue = Integer.parseInt( hex, 16 );

        setSystemId(bytes);
    }

    public String getFMTSystemId()
    {
        final byte[]         bytes = getSystemId(); // never null
        final StringBuilder    sb    = new StringBuilder();
        boolean             first = true;

        for( byte b:bytes ) {
            if( first ) { first = false; }
            else { sb.append( "-" ); }

            sb.append( String.format("%02X", b) );
            }

        return sb.toString();
    }

    public String getMiiCreator()
    {
        return prop.getProperty( DEFAULT_CREATOR, "" );
    }

    public void setMiiCreator( String creator )
    {
        prop.setProperty( DEFAULT_CREATOR, creator.trim() );
    }

    // TODO:
    // Store LookAndFeel
    // Store custom LookAndFeel(s) paths ?

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append( "Preferences [getSystemId()=" );
        //builder.append( String.format( "%08X", getSystemId() ) );
        builder.append( getFMTSystemId() );
        builder.append( ", getMiiCreator()=" );
        builder.append( getMiiCreator() );
        builder.append( "]" );
        return builder.toString();
    }

//    public static void main(String[]args) throws IOException
//    {
//        Preferences instance = new Preferences();
//        instance.load();
//
//        System.out.println( instance );
//    }
}
